@file:JvmName("BooleanUtils")

package com.matuw.android.util.extensions

fun Boolean?.toInt() = if (this == true) 1 else 0

/**
 * Executes and returns the value of [block] if [this] is true, otherwise returns null.
 */
inline fun <R> Boolean?.then(block: () -> R) = if (this == true) block() else null
fun <R> Boolean?.then(value: R) = if (this == true) value else null

inline fun <R> Boolean?.otherwise(block: () -> R) = if (this == null || !this) block() else null

fun <R> Boolean?.otherwise(value: R) = if (this == null || !this) value else null

fun <R> Boolean.either(valueIfTrue: R, valueIfFalse: R) = if (this) valueIfTrue else valueIfFalse

inline fun <R> Boolean.either(valueIfTrue: () -> R, valueIfFalse: () -> R) = if (this) valueIfTrue() else valueIfFalse()